/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.aptech.struts2.action;

import com.aptech.business.IAccountManager;
import com.aptech.domain.JPA.Account;
import com.aptech.domain.JPA.EventRequest;
import com.aptech.business.IEventManager;
import com.aptech.domain.model.AccountDO;
import com.aptech.util.HibernateUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 *
 * @author Admin
 */
public class AccountCRUDAction extends ActionSupport implements ModelDriven<AccountCRUDActionModel> {
    private IAccountManager accountManager;
    private IEventManager eventManager;
    public void setEventManager(IEventManager eventManager) {
        this.eventManager = eventManager;
    }
    AccountCRUDActionModel model = new AccountCRUDActionModel();
    public AccountCRUDActionModel getModel() {
        return model;
    }

    /**
     * @param accountManager the accountManager to set
     */
    public void setAccountManager(IAccountManager accountManager) {
        this.accountManager = accountManager;
    }
    public String executeListAllAccount() {
        try {
            List<AccountDO> accountList = accountManager.getAllAccount();
            AccountDO account = accountList.get(0);
            Map<String,String> json = new HashMap<String,String>();
            for (Iterator it = accountList.iterator(); it.hasNext();) {
                AccountDO acc = (AccountDO)it.next();
                json.put(acc.getFamilyName() + " " + acc.getGivenName(), acc.getUsername());
            }
            model.setAccountList(accountList);
            model.setAccount(account);
            model.setJson(json);
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
        return SUCCESS;
    }
    public String executeJsonAccount() {
        try {
            List<AccountDO> accountList = accountManager.getAllAccount();
            AccountDO account = accountList.get(0);
            Map<String,String> json = new HashMap<String,String>();
            for (Iterator it = accountList.iterator(); it.hasNext();) {
                AccountDO acc = (AccountDO)it.next();
                json.put(acc.getFamilyName() + " " + acc.getGivenName(), acc.getUsername());
            }
            model.setJson(json);
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
        return SUCCESS;
    }
    public String executeSaveAccount() {
        try {
            if (model.getAccount() != null) {
                if(!accountManager.isUsernameExist(
                        model.getAccount().getUsername())) {
                    AccountDO account = model.getAccount();
                    account.setAuthority("EMPLOYEE");
                    //account = accountManager.saveAccount(account);
                    //model.setAccount(account);
                } else {
                    addActionError("Ten tai khoan da ton tai");
                    return INPUT;
                }
            } else {
                return INPUT;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
        addActionMessage("Dang ky thanh cong");
        return SUCCESS;
    }
    public String executeSearchAccount() {
        try {
            if(model.getAccount() != null) {
                String username = model.getAccount().getUsername();
                AccountDO account = accountManager.getAccountByUsername(username);
                model.setAccount(account);
            } else {
                return INPUT;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
        return SUCCESS;
    }
    public String loginAccount() {
        try {
            if(model.getAccount() != null) {
                AccountDO account = accountManager.getAccountByUsername(
                        model.getAccount().getUsername());
                model.setAccount(account);
            } else {
                addFieldError("account.username", "Co loi xay ra.");
                return INPUT;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
        return SUCCESS;
    }
    public String executeRedirectLogin() {
        try {
           // Set session after login
            String username = SecurityContextHolder.getContext().getAuthentication().getName();
            Map sessions = ActionContext.getContext().getSession();
            sessions.put("username", username);
            Transaction tx = null;
            Session session = HibernateUtil.getInstance().openSession();
            tx = session.beginTransaction();
            Criteria ct = session.createCriteria(Account.class);
            ct.add(Restrictions.like("username", username));
            Account account = (Account)ct.uniqueResult();
            Set<EventRequest> eventRequestList = account.getEventRequests();
            List tmp = new ArrayList();
            for (EventRequest eventRequest : eventRequestList) {
                tmp.add(eventRequest.getEvent().getEventId());

            }
            sessions.put("EVENT_REQUEST_LIST", tmp);
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
        return SUCCESS;
    }
}
